const articleService = require('@/services/article.service')
const JsonResult = require('@/utils/httpResponse.util')
const { apiSetApprovelArcReult } = require('@/http')
class ArticleController {
  async findPage (req, response) {
    const { pageNo, pageSize, searchKeywords, status } = req.query
    try {
      const data = await articleService.findPage(pageNo, pageSize, searchKeywords, status)
      JsonResult.success({
        req,
        response,
        data,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }

  async deleteById (req, response) {
    const articleId = req.query.id
    try {
      const data = await articleService.delete(articleId)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        message: '删除成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '删除失败' })
    }
  }

  async approveArticle (req, response) {
    const { articleId, status, reason } = req.body
    try {
      const data = await articleService.approve(articleId, status)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      apiSetApprovelArcReult(articleId, status, reason)
      JsonResult.success({
        req,
        response,
        message: '审核成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '审核失败' })
    }
  }
}

module.exports = new ArticleController()
